package com.iplant.mes.controller.exc;

import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.mes.service.EXCService;
import com.iplant.mes.service.po.exc.EXCProductRepair;
import com.iplant.mes.service.po.sfc.SFCWorkpieceRecord;
import com.iplant.pdm.service.po.bop.FPCRoutePartPoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/EXCProductRepair")
public class EXCProductRepairController extends BasicController<EXCProductRepair> {

    @Autowired
    EXCService mEXCService;

    public EXCProductRepairController() {
        super(EXCProductRepair.class);
    }

    /**
     * 提交返工返修单
     */
    @PostMapping("/Submit")
    public Object Submit(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            EXCProductRepair wEXCProductRepair = CloneTool.Clone(wParam.get("data"), EXCProductRepair.class);
            int wStatus = StringUtils.parseInt(wParam.get("Status"));
            if (wEXCProductRepair == null || wEXCProductRepair.PartPointList == null
                    || wEXCProductRepair.PartPointList.size() <= 0 || wEXCProductRepair.WorkpieceNoList == null
                    || wEXCProductRepair.WorkpieceNoList.size() <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mEXCService.EXC_SubmitProductRepair(wLoginUser, wEXCProductRepair,
                    wStatus);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("Submit", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 根据订单ID查询可返修的工件列表
     */
    @GetMapping("/QueryWorkpieceCodeList")
    public Object QueryWorkpieceCodeList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            if (wOrderID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<SFCWorkpieceRecord>> wServiceResult =
                    mEXCService.EXC_QueryWorkpieceCodeList(wLoginUser, wOrderID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error("QueryWorkpieceCodeList", ex);
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 通过工件号查询返修工序列表
     */
    @GetMapping("/QueryRepairPartPointList")
    public Object QueryRepairPartPointList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            String wWorkpieceNos = StringUtils.parseString(request.getParameter("WorkpieceNos"));

            if (StringUtils.isEmpty(wWorkpieceNos)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<List<FPCRoutePartPoint>> wServiceResult =
                    mEXCService.EXC_QueryRepairPartPointList(wLoginUser, wWorkpieceNos);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }
}
